﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.IO.IsolatedStorage;

namespace KeyDoor
{
    public partial class MainPage : PhoneApplicationPage
    {
        private IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            String appVersion = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split('=')[1].Split(',')[0];

            txtVersion.Text = "App version: " + appVersion;
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }

            var ib = new ImageBrush();
            var bi = new BitmapImage(new Uri("/Images/White/search.png", UriKind.Relative));

            ib.ImageSource = bi;
            btnPostcodeSearch.Background = ib;
        }

        private void btnPostcodeSearch_Click(object sender, RoutedEventArgs e)
        {
            string postcode = txtPostcode.Text.Trim();

            if (String.IsNullOrWhiteSpace(postcode))
            {
                MessageBox.Show("Please enter a valid postcode.", "ERROR", MessageBoxButton.OK);
            } else
                NavigationService.Navigate(new Uri("/Searcher/index.xaml?postcode=" + txtPostcode.Text, UriKind.Relative));

        }

        private void txtTeam1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri("http://twitter.com/alexerax", UriKind.Absolute)
                };

                task.Show();
            }
            catch { }
        }

        private void txtTeam2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri("http://twitter.com/dylanmaryk", UriKind.Absolute)
                };

                task.Show();
            }
            catch { }
        }

        private void txtTeam3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri("http://twitter.com/mussics", UriKind.Absolute)
                };

                task.Show();
            }
            catch { }
        }

        private void txtTeam4_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri("http://twitter.com/phazonoverload", UriKind.Absolute)
                };

                task.Show();
            }
            catch { }
        }

        private void txtTeam5_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var task = new Microsoft.Phone.Tasks.WebBrowserTask
                {
                    Uri = new Uri("http://twitter.com/pikelk", UriKind.Absolute)
                };

                task.Show();
            }
            catch { }
        }

        private void txtPostcode_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            txtPostcode.Text = txtPostcode.Text.Replace("Enter your postcode", "");
        }

        private void btnReportBug_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("This feature has not yet been implemented.", "Feature Not Implemented", MessageBoxButton.OK);
        }

        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("This feature has not yet been implemented.", "Feature Not Implemented", MessageBoxButton.OK);
        }

        private void lstBoxRecents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstBoxRecents.SelectedIndex != -1)
            {
                Controls.addressListItem bx;
                bx = (Controls.addressListItem)lstBoxRecents.SelectedItem;
                NavigationService.Navigate(new Uri("/Searcher/index.xaml?postcode=" + bx.lblPostcode.Text, UriKind.Relative));
            }
        }

        private void lstBoxFavs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstBoxFavs.SelectedIndex != -1)
            {
                Controls.addressListItem bx;
                bx = (Controls.addressListItem)lstBoxFavs.SelectedItem;
                NavigationService.Navigate(new Uri("/Searcher/index.xaml?postcode=" + bx.lblPostcode.Text, UriKind.Relative));
            }
        }

        private void PhoneApplicationPage_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                string[] recents = null;
                settings.TryGetValue<string[]>("recentValues", out recents);
                lstBoxRecents.Items.Clear();

                foreach (string line in recents)
                {
                    if (line != null)
                    {
                        string[] recent = line.Split('&');

                        Controls.addressListItem lst = new Controls.addressListItem();
                        lst.lblAddress.Text = recent[1];
                        lst.lblPostcode.Text = recent[0].ToUpper();

                        lstBoxRecents.Items.Add(lst);
                    }
                }
            }
            catch (Exception ex)
            {
                string[] recents = new string[5];
                settings.Remove("recentValues");
                settings.Add("recentValues", recents);
                settings.Save();
            }

            try
            {
                string[] recents = null;
                settings.TryGetValue<string[]>("favs", out recents);
                lstBoxFavs.Items.Clear();

                foreach (string line in recents)
                {
                    if (line != null)
                    {
                        string[] recent = line.Split('&');

                        Controls.addressListItem lst = new Controls.addressListItem();
                        lst.lblAddress.Text = recent[1];
                        lst.lblPostcode.Text = recent[0].ToUpper();

                        lstBoxFavs.Items.Add(lst);
                    }
                }
            }
            catch (Exception ex)
            {
                string[] recents = new string[5];
                settings.Remove("favs");
                settings.Add("favs", recents);
                settings.Save();
            }
        }
    }
}